﻿//Copyright 2010 http://tv4home.codeplex.com
//This file is part of TV4Home.WebInterface.UI.
//TV4Home.WebInterface.UI is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
//TV4Home.WebInterface.UI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//You should have received a copy of the GNU General Public License along with TV4Home.WebInterface.UI If not, see http://www.gnu.org/licenses/
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web;
using TV4Home.Server.TVEInteractionLibrary.Interfaces;
using System.IO;

namespace TV4Home.WebInterface.UI.WebService
{
    public class TV4HomeWebService : ITVEInteraction
    {
        #region Fields
        private static ITVEInteraction _service;
        #endregion

        #region Properties
        public static ITVEInteraction ServiceInstance
        {
            get
            {
                if (_service == null || ((ICommunicationObject)_service).State == CommunicationState.Faulted)
                {
                    _service = ChannelFactory<ITVEInteraction>.CreateChannel(new NetNamedPipeBinding() { MaxReceivedMessageSize = 10000000 }, new EndpointAddress("net.pipe://localhost/TV4Home.Server.CoreService/TVEInteractionService"));
                }

                return _service;
            }
        }
        #endregion

        #region Service Methods

        public bool TestConnectionToTVService()
        {
            return ServiceInstance.TestConnectionToTVService();
        }

        public void AddSchedule(int channelId, string title, DateTime startTime, DateTime endTime, int scheduleType)
        {
            ServiceInstance.AddSchedule(channelId, title, startTime, endTime, scheduleType);
        }

        public void AddScheduleDetailed(int channelId, string title, DateTime startTime, DateTime endTime, int scheduleType, int preRecordInterval, int postRecordInterval, string directory, int priority)
        {
            ServiceInstance.AddScheduleDetailed(channelId, title, startTime, endTime, scheduleType, preRecordInterval, postRecordInterval, directory, priority);
        }

        public WebVirtualCard SwitchTVServerToChannelAndGetVirtualCard(string userName, int channelId)
        {
            return ServiceInstance.SwitchTVServerToChannelAndGetVirtualCard(userName, channelId);
        }

        public string SwitchTVServerToChannelAndGetStreamingUrl(string userName, int channelId)
        {
            return ServiceInstance.SwitchTVServerToChannelAndGetStreamingUrl(userName, channelId);
        }

        public string SwitchTVServerToChannelAndGetTimeshiftFilename(string userName, int channelId)
        {
            return ServiceInstance.SwitchTVServerToChannelAndGetTimeshiftFilename(userName, channelId);
        }

        public void SendHeartbeat(string userName)
        {
            ServiceInstance.SendHeartbeat(userName);
        }

        public bool CancelCurrentTimeShifting(string userName)
        {
            return ServiceInstance.CancelCurrentTimeShifting(userName);
        }

        public List<WebChannelDetailed> GetChannelsDetailed(int groupId)
        {
            return ServiceInstance.GetChannelsDetailed(groupId);
        }

        public List<WebChannelDetailed> GetChannelsDetailedByRange(int groupId, int startIndex, int count)
        {
            return ServiceInstance.GetChannelsDetailedByRange(groupId, startIndex, count);
        }

        public List<WebChannelBasic> GetChannelsBasic(int groupId)
        {
            return ServiceInstance.GetChannelsBasic(groupId);
        }

        public List<WebChannelBasic> GetChannelsBasicByRange(int groupId, int startIndex, int count)
        {
            return ServiceInstance.GetChannelsBasicByRange(groupId, startIndex, count);
        }

        public int GetChannelCount(int groupId)
        {
            return ServiceInstance.GetChannelCount(groupId);
        }

        public List<WebRecording> GetRecordings()
        {
            return ServiceInstance.GetRecordings();
        }

        public List<WebSchedule> GetSchedules()
        {
            return ServiceInstance.GetSchedules();
        }

        public WebChannelDetailed GetChannelDetailedById(int channelId)
        {
            return ServiceInstance.GetChannelDetailedById(channelId);
        }

        public WebChannelBasic GetChannelBasicById(int channelId)
        {
            return ServiceInstance.GetChannelBasicById(channelId);
        }

        public WebProgramDetailed GetProgramDetailedById(int programId)
        {
            return ServiceInstance.GetProgramDetailedById(programId);
        }

        public WebProgramBasic GetProgramBasicById(int programId)
        {
            return ServiceInstance.GetProgramBasicById(programId);
        }

        public List<WebProgramDetailed> GetProgramsDetailedForChannel(int channelId, DateTime startTime, DateTime endTime)
        {
            return ServiceInstance.GetProgramsDetailedForChannel(channelId, startTime, endTime);
        }

        public List<WebProgramBasic> GetProgramsBasicForChannel(int channelId, DateTime startTime, DateTime endTime)
        {
            return ServiceInstance.GetProgramsBasicForChannel(channelId, startTime, endTime);
        }

        public bool GetProgramIsScheduledOnChannel(int channelId, int programId)
        {
            return ServiceInstance.GetProgramIsScheduledOnChannel(channelId, programId);
        }

        public bool GetProgramIsScheduled(int programId)
        {
            return ServiceInstance.GetProgramIsScheduled(programId);
        }

        public List<WebProgramDetailed> SearchProgramsDetailed(string searchTerm)
        {
            return ServiceInstance.SearchProgramsDetailed(searchTerm);
        }

        public List<WebProgramBasic> SearchProgramsBasic(string searchTerm)
        {
            return ServiceInstance.SearchProgramsBasic(searchTerm);
        }

        public List<WebChannelGroup> GetGroups()
        {
            return ServiceInstance.GetGroups();
        }

        public WebChannelGroup GetGroupById(int groupId)
        {
            return ServiceInstance.GetGroupById(groupId);
        }

        public void CancelSchedule(int programId)
        {
            ServiceInstance.CancelSchedule(programId);
        }

        public void DeleteSchedule(int scheduleId)
        {
            ServiceInstance.DeleteSchedule(scheduleId);
        }

        public List<WebCard> GetCards()
        {
            return ServiceInstance.GetCards();
        }

        public List<WebVirtualCard> GetActiveCards()
        {
            return ServiceInstance.GetActiveCards();
        }

        public List<WebRtspClient> GetStreamingClients()
        {
            return ServiceInstance.GetStreamingClients();
        }

        public List<WebUser> GetActiveUsers()
        {
            return ServiceInstance.GetActiveUsers();
        }

        public WebProgramDetailed GetCurrentProgramOnChannel(int channelId)
        {
            return ServiceInstance.GetCurrentProgramOnChannel(channelId);
        }

        public WebProgramDetailed GetNextProgramOnChannel(int channelId)
        {
            return ServiceInstance.GetNextProgramOnChannel(channelId);
        }

        public string ReadSettingFromDatabase(string tagName)
        {
            return ServiceInstance.ReadSettingFromDatabase(tagName);
        }

        public void WriteSettingToDatabase(string tagName, string value)
        {
            ServiceInstance.WriteSettingToDatabase(tagName, value);
        }

        public bool FinishStream(string identifier)
        {
            return ServiceInstance.FinishStream(identifier);
        }
        public bool FinishStreamAndCancelTimeshifting(string userName)
        {
            return ServiceInstance.FinishStreamAndCancelTimeshifting(userName);
        }
        public List<WebTranscoderProfile> GetTranscoderProfiles()
        {
            return ServiceInstance.GetTranscoderProfiles();
        }
        public WebTranscoderProfile GetTranscoderProfileByName(string name)
        {
            return ServiceInstance.GetTranscoderProfileByName(name);
        }
        public List<WebTranscoderProfile> GetTranscoderProfilesForTarget(string target)
        {
            return ServiceInstance.GetTranscoderProfilesForTarget(target);
        }
        public WebTranscodingInfo GetTranscodingInfo(string identifier)
        {
            return ServiceInstance.GetTranscodingInfo(identifier);
        }
        public bool InitRecordingStream(string identifier, int recordingId, string profile, int startPosition)
        {

            return ServiceInstance.InitRecordingStream(identifier, recordingId, profile, startPosition);
        }
        public bool SwitchTVServerToChannelAndInitStream(string userName, int channelId, string profile)
        {
            return ServiceInstance.SwitchTVServerToChannelAndInitStream(userName, channelId, profile);
        }
        public Dictionary<int,WebChannelState> GetAllChannelStatesForGroup(int groupId, string userName)
        {
        return ServiceInstance.GetAllChannelStatesForGroup(groupId,userName);
        }
        public WebChannelState GetChannelState(int channelId, string userName)
        {
            return ServiceInstance.GetChannelState(channelId, userName);
        }
        public Stream GetChannelLogo(int channelId)
        {
            return ServiceInstance.GetChannelLogo(channelId);
        }



        #endregion
    }
}
